var prefix = api + "/admin/log";

$(function () {
	var datas = dictByType('log_type')
	renderDictFilter('#type', 'log_type')
	var deleteFlag = permissions.sys_log_del
	//没有清空权限的话删除清空按钮
	if (!deleteFlag) {
		$('#clean').remove()
	}
	var options = {
		url: prefix + "/page",
		// cleanUrl: prefix + "/clean",
		updateUrl: "/pages/sysadm/syslog/detail",
		removeUrl: prefix + "/{id}",
		// exportUrl : prefix + "/export",
		sortName: "createTime",
		sortOrder: "desc",
		modalName: "操作日志",
		escape: true,
		showPageGo: true,
		rememberSelected: true,
		columns: [{
			title: "序号",
			align: 'center',
			formatter: function (value, row, index) {
				return $.table.serialNumber(index);
			}
		},
		{
			field: 'type',
			title: '类型',
			align: 'center',
			formatter: function (value, row, index) {
				return $.table.selectDictLabel(datas, value);
				// if (value == 0) {
				// 	return '<span class="badge badge-primary">成功</span>';
				// } else if (value == 9) {
				// 	return '<span class="badge badge-danger">失败</span>';
				// }
			}
		},
		{
			field: 'title',
			align: 'center',
			title: '标题',
			sortable: true,
		},
		{
			field: 'remoteAddr',
			align: 'center',
			title: 'Ip地址'
		},
		{
			field: 'method',
			align: 'center',
			title: '请求方式'
		},
		{
			title: '客户端',
			align: 'center',
			field: 'serviceId'
		},
		{
			title: '请求时间',
			align: 'center',
			field: 'time'
		},
		{
			field: 'createTime',
			align: 'center',
			title: '创建时间',
			sortable: true,
		},
		{
			title: '操作',
			align: 'center',
			formatter: function (value, row, index) {
				var actions = [];
				if (deleteFlag) {
					actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a> ');
				}
				actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="$.operate.view(\'' + row.id + '\')"><i class="fa fa-search"></i>详细</a>');
				return actions.join('');
			}
		}]
	};
	$.table.init(options);
});


function clean() {
	$('#clean-form').show()
	dateRange()
	layer.open({
		type: 1,
		area: ['400px', '230px'],
		fix: false,
		//不固定
		maxmin: true,
		shade: 0.3,
		title: '选择要清空的时间段',
		content: $('#clean-form'),
		btn: ['<i class="fa fa-check"></i> 确定', '<i class="fa fa-remove"></i> 取消'],
		// 弹层外区域关闭
		shadeClose: true,
		btn1: function (index, layero) {
			var dateRange = $('#dateRange').val().split(' 至 ')
			var data = {
				startTime: dateRange[0]+ ' 00:00:00',
				endTime: dateRange[1]+ ' 23:59:59',
			}
			if (data.startTime == '' || data.endTime == undefined) {
				$.modal.msgWarning("请选择要清空的时间段");
				return false;
			}
			$.ajax({
				url: api + "/admin/log/clean?startTime=" + data.startTime + "&endTime=" + data.endTime,
				type: 'delete',
				success: function (result) {
					// $.modal.closeAll();
					layer.close(index);
					$('#clean-form').hide()
					$.modal.alertSuccess("删除成功");
					$.table.refresh();
				}
			});
		},
		end:function (index, layero) {
			layer.close(index);
			$('#clean-form').hide()
		}
	});
}

function dateRange() {
	layui.use('laydate', function () {
		var laydate = layui.laydate;
		var startDate = laydate.render({
			elem: '#dateRange',
			theme: 'molv',
			trigger: 'click',
			range: '至',
			done: function (value, date) {

			}
		});
	});
}